﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Recipe
{
    public class Recipe:IRecipe
    {
        public void AddRecipe(RecipeData Recipe, ref Model.NutriLeafDBEntities context)
        {
           //doubt
        }

        public void AddRecipe(string RecipeName, int categoryId, string description, int userId, bool approved, ref Model.NutriLeafDBEntities context)
        {
            Model.Recipe recipie = new Model.Recipe();
            recipie.RecipeName = RecipeName;
            recipie.RecipeCategoryID = categoryId;
            recipie.RecipeDescription = description;
            recipie.UserID = userId;
            recipie.Approved = approved;
            context.Recipes.AddObject(recipie);
            context.SaveChanges();
        }

        public List<Model.Recipe> SuggestRecipes(int userId, ref Model.NutriLeafDBEntities context)
        {
            return context.Recipes.Where(p => p.UserID == userId).ToList();
        }

        public void RemoveRecipe(int RecipeId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Recipes.Where(p => p.RecipeID == RecipeId).First());
            context.SaveChanges();
        }

        public void UpdateRecipe(RecipeData RecipeData, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void UpdateRecipe(Model.Recipe Recipes, ref Model.NutriLeafDBEntities context)
        {
            Model.Recipe recipie = context.Recipes.Where(p => p.RecipeID == Recipes.RecipeID).First();
            recipie = Recipes;
            context.SaveChanges();
        }

        public RecipeData GetRecipeData(int RecipeId, ref Model.NutriLeafDBEntities context)
        {
            return new RecipeData();
       
        }

        public List<General.StringIdPair> GetRecipeNames(string startsWith, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Recipe> r = context.Recipes.Where(p=>p.RecipeName.StartsWith(startsWith)).ToList();
            List<General.StringIdPair> idpair = new List<General.StringIdPair>();
            foreach (Model.Recipe rs in r) {
                General.StringIdPair s = new General.StringIdPair();
                s.id = rs.RecipeID;
                s.name = rs.RecipeName;
                idpair.Add(s);
            }
            return idpair;
            
        }

        public void ApproveRecipe(int recipeId, ref Model.NutriLeafDBEntities context)
        {
            Model.Recipe r = context.Recipes.Where(p => p.RecipeID == recipeId).First();
            r.Approved = true;
            context.SaveChanges();
        }

        public List<Model.Recipe> GetAllNotApprovedRecipe(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            return context.Recipes.Where(p => p.RecipeCategoryID == categoryId && p.Approved == false).ToList();   
        }
    }
}